ggmap

Jan-Philipp Kolb

04/13/2015

Road maps

A road map is one of the most widely used map types.

Library ggmap

library(ggmap)
qmap("Mannheim")

MA_map <- qmap("Mannheim",zoom=14)

Other zoom level

qmap(location = 'Mannheim', zoom = 12)

Get closer

qmap(location = 'Mannheim', zoom = 13)

Get very close

qmap(location = 'Mannheim', zoom = 20)

ggmap - source

qmap(location = 'Mannheim', zoom = 14, source="osm")

ggmap - maptype

qmap(location = 'Mannheim', zoom = 14, maptype="satellite")

ggmap - maptype

qmap(location = 'Mannheim', zoom = 14, maptype="hybrid")

ggmap - maptype

qmap(location = 'Mannheim', zoom = 14,
 maptype="toner",source="stamen")

Other ways to get a map

MAmap <- get_googlemap("Mannheim")
ggmap(MAmap)

Geocoding

Geocoding (…) uses a description of a location, most typically a postal address or place name, to find geographic coordinates from spatial reference data …

Wikipedia - Geocoding

geocode("Mannheim Wasserturm")
## Warning: geocode failed with status ZERO_RESULTS, location = "Mannheim
## University"
lon lat
NA NA

Geocoding - points of interest

POI1 <- geocode("B2, 1 Mannheim")
POI2 <- geocode("Hbf Mannheim")
POI3 <- geocode("Wasserturm Mannheim")

POI1;POI2;POI3
##       lon      lat
## 1 8.47955 49.49671
##       lon      lat
## 1 8.47955 49.49671
##       lon      lat
## 1 8.47955 49.49671

Geocoding - with a loop

POI <- c("B2, 1 Mannheim","Hbf Mannheim",
         "Wasserturm Mannheim")

ListPOI <- data.frame(lat=NA,lon=NA)

for ( i in 1:length(POI)){
  geoPOI <- geocode(POI[i])
  ListPOI[i,"lat"] <-  geoPOI$lat 
  ListPOI[i,"lon"] <-  geoPOI$lon 
}

Points in map

Article by David Kahle and Hadley Wickham on the usage of ggmap.

MA_map +
geom_point(aes(x = lon, y = lat),
data = ListPOI)

More about adding points

pic

ggmap - adding colors

ListPOI$color <- c("A","B","C")
MA_map +
geom_point(aes(x = lon, y = lat,col=color),
data = ListPOI)

ggmap - bigger dots

ListPOI$size <- c(10,20,30)
MA_map +
geom_point(aes(x = lon, y = lat,col=color,size=size),
data = ListPOI)

Cheatsheet

pic

Get the distance between 2 points

mapdist("Q1, 4 Mannheim","B2, 1 Mannheim")
##             from             to   m    km     miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 806 0.806 0.5008484     217 3.616667
##        hours
## 1 0.06027778
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="walking")
##             from             to   m    km     miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 546 0.546 0.3392844     421 7.016667
##       hours
## 1 0.1169444

Get another distance

mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="bicycling")
##             from             to   m    km    miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 555 0.555 0.344877     215 3.583333
##        hours
## 1 0.05972222